package itheima.com.Day14.semaphore;

import java.util.concurrent.Semaphore;

public class MyRunnable implements Runnable {
   //创建对象  参数为最大可同时进行的线程数
    Semaphore semaphore = new Semaphore(2);
    @Override
    public void run() {
//        获取通行证
        try {
            semaphore.acquire();
            //可通行后进行睡眠
            System.out.println("获得了通行证");
            Thread.sleep(1000);
            System.out.println("归还了通行证");
            //归还通行证
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
